writeLines("************************************************************************************************************************")

# 	Filename: exit_driver.R
# 	Description: Driver script to produce results based on regressions where leaderexitsoffice is the dependent variable.
writeLines("Description: Driver script to produce results from regressions where leaderexitsoffice is the dependent variable.")
# 	Author: Barry Hashimoto
# 	Date: December 2019
# 	For: Barry Hashimoto, "Autocratic Consent to International Law: the Case of the International Criminal Court's Jurisdiction," International Organization.

timestamp()
set.seed(100)
options(scipen=999) # Prevent scientific formatting of numbers.

suppressPackageStartupMessages({library(Amelia); library(Zelig); library(brglm); library(MatchIt); library(cem); library(doBy); library(mvtnorm); library(plyr); library(mitools); library(lattice); library(caret); library(memisc); library(arm); library(mitools); library(AICcmodavg); library(plm); library(mgcv); library(parallel); library(future)})

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part I:  Analyze leader exit using the multiply imputed data sets.")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("I.A. Analyze leader exit for autocracies using the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only autocracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]} 
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy== 0,]} 
  # Run entire script with statistical analysis. Long-ish runtime!
  source("exitMI_source.R", echo = F, spaced = T)

writeLines(""); writeLines("") 
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("I.B. Analyze leader exit for democracies using the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only democracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]}
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy== 1,]}
  # Run entire script with statistical analysis. Long-ish runtime!
  source("exitMI_source.R", echo = F, spaced = T)

writeLines(""); writeLines("") 
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part II:  Analyze leader exit for only capital-eligible states using the multiply imputed data sets.")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("II.A. Analyze leader exit for only the capital-eligible autocracies using the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only aid eligible autocracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]}  
  mean.aid <- summaryBy(capital.scaled ~ ccode, data = full$imputations[[1]], FUN = mean)
  aid.eligible.states <- mean.aid[mean.aid$capital.scaled>0, 1]
  for(z in 1:10){full$imputations[[z]] <- full$imputations[[z]][which(full$imputations[[z]]$ccode %in% aid.eligible.states),]}
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy==0,]}
  # Run entire script with statistical analysis.
  source("exitMI_source.R", echo = F, spaced = T)

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("II.B. Analyze leader exit for only the capital-eligible democracies using the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only aid eligible democracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]}
  mean.aid <- summaryBy(capital.scaled ~ ccode, data = full$imputations[[1]], FUN = mean)
  aid.eligible.states <- mean.aid[mean.aid$capital.scaled>0, 1]
  for(z in 1:10){full$imputations[[z]] <- full$imputations[[z]][which(full$imputations[[z]]$ccode %in% aid.eligible.states),]}
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy== 1,]}
  # Run entire script with statistical analysis. 
  source("exitMI_source.R", echo = F, spaced = T) 

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part III:  Analyze leader exit using the unimputed data set, applying listwise deletion.")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("III.A. Analyze leader exit for autocracies using the unimputed data set, applying listwise deletion.")

  rm(list=ls())
  load("iccdata.RData")
  RegimeOption <- 0
  source("exitLD_source.R", echo = F, spaced = T)   # Run entire script with statistical analysis.

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("III.B. Analyze leader exit for democracies using the unimputed data set, applying listwise deletion.")

  rm(list=ls())
  load("iccdata.RData")
  RegimeOption <- 1
  source("exitLD_source.R", echo = F, spaced = T)   # Run entire script with statistical analysis.

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
 writeLines("End of exit_driver.R script.")
 timestamp()
 writeLines(""); writeLines("")
writeLines("************************************************************************************************************************") 
 
 
